//
//  FSAlertView.m
//  TrigFShop
//
//  Created by lqq on 17/1/23.
//  Copyright © 2017年 szjxmac. All rights reserved.
//

#import "FSAlertView.h"
#import "NSError+Extension.h"
@interface FSAlertView ()
@property (weak, nonatomic) IBOutlet UIImageView *statusImageView;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UIImageView *loadingImageView;
@property (weak, nonatomic) IBOutlet UIView *alertBackView;

@end
@implementation FSAlertView

+ (FSAlertView *)sharedInstance
{
    static dispatch_once_t t;
    static FSAlertView *sharedInstance = nil;
    dispatch_once(&t, ^{
        sharedInstance = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([FSAlertView class]) owner:nil options:nil] firstObject];
        sharedInstance.backgroundColor = [UIColor clearColor];
        NSMutableArray *animationImages = [NSMutableArray array];
        for (int i = 1 ;  i<= 9; i++) {
            NSString *imageName = [NSString stringWithFormat:@"run_%d",i];
            [animationImages safeAddObject:[UIImage imageNamed:imageName]];
        }
        sharedInstance.loadingImageView.animationImages = animationImages;
        sharedInstance.loadingImageView.animationDuration = 0.5;
        sharedInstance.alertBackView.layer.cornerRadius = 4;
        sharedInstance.alertBackView.layer.masksToBounds = YES;
        UITapGestureRecognizer *hiddeSelfTap = [[UITapGestureRecognizer alloc] initWithTarget:sharedInstance action:@selector(hideen)];
        [sharedInstance addGestureRecognizer:hiddeSelfTap];
    });
    return sharedInstance;
}


- (void)showMessage:(NSString *)message type:(AlertType)alertType
{
    UIView *window = [UIApplication sharedApplication].keyWindow;
    self.alpha = 1;
    
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideen) object:nil];
    
    switch (alertType) {
        case AlertTypeSuccess:
        {
            self.statusImageView.hidden =  NO;
            self.messageLabel.hidden = NO;
            [self.loadingImageView stopAnimating];
            self.loadingImageView .hidden = YES;
            self.statusImageView.image = [UIImage imageNamed:@"ic_120_yes_normal_b"];
            self.messageLabel.text = message;
            [self performSelector:@selector(hideen) withObject:nil afterDelay:1.5];

        }
            break;
        case AlertTypeError:
        case AlertTypeRemind:
        {
            self.statusImageView.hidden =  NO;
            self.messageLabel.hidden = NO;
            [self.loadingImageView stopAnimating];
            self.loadingImageView .hidden = YES;
            self.statusImageView.image = [UIImage imageNamed:@"ic_120_no_normal_b"];
            self.messageLabel.text = message;
            [self performSelector:@selector(hideen) withObject:nil afterDelay:1.5];
        }
            break;
        case AlertTypeLoading:
        {
            self.statusImageView.hidden =  YES;
            self.messageLabel.hidden = NO;
            [self.loadingImageView startAnimating];
            self.loadingImageView .hidden = NO;
            self.messageLabel.text = message;
        }
            break;
        default:
            break;
    }
    self.frame = window.bounds;
    [window addSubview:self];
    
    
}

- (void)hideen
{
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)showLoading:(NSString *)message
{
    [self showMessage:message type:AlertTypeLoading];
}
- (void)showSuccess:(NSString *)message
{
    [self showMessage:message type:AlertTypeSuccess];
}
- (void)showError:(NSError *)error
{
    [self showMessage:error.errorMsg type:AlertTypeError];
}
- (void)showErrorMessage:(NSString *)errorMessage
{
    [self showMessage:errorMessage type:AlertTypeError];
}

- (void)showRemind:(NSString *)message
{
    [self showMessage:message type:AlertTypeRemind];
}



@end
